#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#	tweerminal v0.9
#		programa tipo-comando para administrar mensajes en twitter desde una terminal.
#
#	Copyleft 2010 Qcho
#
#	Este programa es software libre: usted puede redistribuirlo y/o modificarlo conforme a los términos de la Licencia Pública General de
#	GNU publicada por la Fundación para el Software Libre, ya sea la version 3 de esta Licencia o (a su elección) cualquier version
#	posterior.
#	Este programa se distribuye con el deseo de que le resulte útil, pero SIN GARANTÍAS DE NINGÚN TIPO; ni siquiera con las garantías
#	implícitas de COMERCIABILIDAD o APTITUD PARA UN PROPÓSITO DETERMINADO.  Para más informacion, consulte la Licencia Pública General de
#	GNU.
#	Junto con este programa, se debería incluir una copia de la Licencia Publica General de GNU.
#	De no ser así, ingrese en <http://www.gnu.org/licenses/>.


import sys
import string
import urllib
import time
import getpass
	
import tw_twitter
from tw_msg import *
from tw_color import *

HTML_CODES = (
	('&quot;', unicode('"', "utf-8")),
	('&amp;', unicode('&', "utf-8")),
	('&lt;', unicode('<', "utf-8")),
	('&gt;', unicode('>', "utf-8")),
	('&nbsp;', unicode(' ', "utf-8")),
	('&Agrave;', unicode('À', "utf-8")),
	('&agrave;', unicode('à', "utf-8")),
	('&Aacute;', unicode('Á', "utf-8")),
	('&aacute;', unicode('á', "utf-8")),
	('&Acirc;', unicode('Â', "utf-8")),
	('&acirc;', unicode('â', "utf-8")),
	('&Atilde;', unicode('Ã', "utf-8")),
	('&atilde;', unicode('ã', "utf-8")),
	('&Auml;', unicode('Ä', "utf-8")),
	('&auml;', unicode('ä', "utf-8")),
	('&Aring;', unicode('Å', "utf-8")),
	('&aring;', unicode('å', "utf-8")),
	('&AElig;', unicode('Æ', "utf-8")),
	('&aelig;', unicode('æ', "utf-8")),
	('&Ccedil;', unicode('Ç', "utf-8")),
	('&ccedil;', unicode('ç', "utf-8")),
	('&ETH;', unicode('Ð', "utf-8")),
	('&eth;', unicode('ð', "utf-8")),
	('&Egrave;', unicode('È', "utf-8")),
	('&egrave;', unicode('è', "utf-8")),
	('&Eacute;', unicode('É', "utf-8")),
	('&eacute;', unicode('é', "utf-8")),
	('&Ecirc;', unicode('Ê', "utf-8")),
	('&ecirc;', unicode('ê', "utf-8")),
	('&Euml;', unicode('Ë', "utf-8")),
	('&euml;', unicode('ë', "utf-8")),
	('&Igrave;', unicode('Ì', "utf-8")),
	('&igrave;', unicode('ì', "utf-8")),
	('&Iacute;', unicode('Í', "utf-8")),
	('&iacute;', unicode('í', "utf-8")),
	('&Icirc;', unicode('Î', "utf-8")),
	('&icirc;', unicode('î', "utf-8")),
	('&Iuml;', unicode('Ï', "utf-8")),
	('&iuml;', unicode('ï', "utf-8")),
	('&micro;', unicode('µ', "utf-8")),
	('&Ntilde;', unicode('Ñ', "utf-8")),
	('&ntilde;', unicode('ñ', "utf-8")),
	('&Ograve;', unicode('Ò', "utf-8")),
	('&ograve;', unicode('ò', "utf-8")),
	('&Oacute;', unicode('Ó', "utf-8")),
	('&oacute;', unicode('ó', "utf-8")),
	('&Ocirc;', unicode('Ô', "utf-8")),
	('&ocirc;', unicode('ô', "utf-8")),
	('&Otilde;', unicode('Õ', "utf-8")),
	('&otilde;', unicode('õ', "utf-8")),
	('&Ouml;', unicode('Ö', "utf-8")),
	('&ouml;', unicode('ö', "utf-8")),
	('&Oslash;', unicode('Ø', "utf-8")),
	('&oslash;', unicode('ø', "utf-8")),
	('&szlig;', unicode('ß', "utf-8")),
	('&THORN;', unicode('Þ', "utf-8")),
	('&thorn;', unicode('þ', "utf-8")),
	('&Ugrave;', unicode('Ù', "utf-8")),
	('&ugrave;', unicode('ù', "utf-8")),
	('&Uacute;', unicode('Ú', "utf-8")),
	('&uacute;', unicode('ú', "utf-8")),
	('&Ucirc;', unicode('Û', "utf-8")),
	('&ucirc;', unicode('û', "utf-8")),
	('&Uuml;', unicode('Ü', "utf-8")),
	('&uuml;', unicode('ü', "utf-8")),
	('&Yacute;', unicode('Ý', "utf-8")),
	('&yacute;', unicode('ý', "utf-8")),
	('&yuml;', unicode('ÿ', "utf-8")),
	('&uml;', unicode('¨', "utf-8")),
	('&macr;', unicode('¯', "utf-8")),
	('&acute;', unicode('´', "utf-8")),
	('&cedil;', unicode('¸', "utf-8")),
	('&iexcl;', unicode('¡', "utf-8")),
	('&iquest;', unicode('¿', "utf-8")),
	('&middot;', unicode('·', "utf-8")),
	('&brvbar;', unicode('¦', "utf-8")),
	('&laquo;', unicode('«', "utf-8")),
	('&raquo;', unicode('»', "utf-8")),
	('&para;', unicode('¶', "utf-8")),
	('&sect;', unicode('§', "utf-8")),
	('&copy;', unicode('©', "utf-8")),
	('&reg;', unicode('®', "utf-8")),
	('&sup1;', unicode('¹', "utf-8")),
	('&sup2;', unicode('²', "utf-8")),
	('&sup3;', unicode('³', "utf-8")),
	('&shy;', unicode(' ', "utf-8")),
	('&times;', unicode('×', "utf-8")),
	('&divide;', unicode('÷', "utf-8")),
	('&frac14;', unicode('¼', "utf-8")),
	('&frac12;', unicode('½', "utf-8")),
	('&frac34;', unicode('¾', "utf-8")),
	('&ordf;', unicode('ª', "utf-8")),
	('&ordm;', unicode('º', "utf-8")),
	('&not;', unicode('¬', "utf-8")),
	('&deg;', unicode('°', "utf-8")),
	('&plusmn;', unicode('±', "utf-8")),
	('&curren;', unicode('¤', "utf-8")),
	('&cent;', unicode('¢', "utf-8")),
	('&pound;', unicode('£', "utf-8")),
	('&yen;', unicode('¥', "utf-8")),
	('&Delta;', unicode('Δ', "utf-8")),
	('&fnof;', unicode('ƒ', "utf-8")),
	('&Omega;', unicode('Ω', "utf-8")),
	('&OElig;', unicode('Œ', "utf-8")),
	('&oelig;', unicode('œ', "utf-8")),
	('&Scaron;', unicode('Š', "utf-8")),
	('&scaron;', unicode('š', "utf-8")),
	('&Yuml;', unicode('Ÿ', "utf-8")),
	('&inodot;', unicode('&inodot;', "utf-8")),
	('&circ;', unicode('ˆ', "utf-8")),
	('&caron;', unicode('&caron;', "utf-8")),
	('&breve;', unicode('&breve;', "utf-8")),
	('&dot;', unicode('&dot;', "utf-8")),
	('&ring;', unicode('&ring;', "utf-8")),
	('&ogon;', unicode('&ogon;', "utf-8")),
	('&tilde;', unicode('˜', "utf-8")),
	('&dblac;', unicode('&dblac;', "utf-8")),
	('&ndash;', unicode('–', "utf-8")),
	('&mdash;', unicode('—', "utf-8")),
	('&dagger;', unicode('†', "utf-8")),
	('&Dagger;', unicode('‡', "utf-8")),
	('&bull;', unicode('•', "utf-8")),
	('&hellip;', unicode('…', "utf-8")),
	('&lsquo;', unicode('‘', "utf-8")),
	('&rsquo;', unicode('’', "utf-8")),
	('&lsquor;', unicode('&lsquor;', "utf-8")),
	('&ldquo;', unicode('“', "utf-8")),
	('&rdquo;', unicode('”', "utf-8")),
	('&ldquor;', unicode('&ldquor;', "utf-8")),
	('&lsaquo;', unicode('‹', "utf-8")),
	('&rsaquo;', unicode('›', "utf-8")),
	('&trade;', unicode('™', "utf-8")),
	('&radic;', unicode('√', "utf-8")),
	('&infin;', unicode('∞', "utf-8")),
	('&int;', unicode('∫', "utf-8")),
	('&part;', unicode('∂', "utf-8")),
	('&ap;', unicode('&ap;', "utf-8")),
	('&ne;', unicode('≠', "utf-8")),
	('&le;', unicode('≤', "utf-8")),
	('&ge;', unicode('≥', "utf-8")),
	('&sum;', unicode('∑', "utf-8")),
	('&permil;', unicode('‰', "utf-8")),
	('&prod;', unicode('∏', "utf-8")),
	('&pi;', unicode('π', "utf-8")),
	('&loz;', unicode('◊', "utf-8"))
)

psswd = None

def _set_tags(original, tags):
	for (i,j) in tags:
		original = original.replace('{'+i+'}', j)
	return original

def _arg(cont,num):
	args = {}
	for i in range(len(sys.argv)):
		if sys.argv[i] in cont:
			for j in range(num):
				try:
					args[j] = sys.argv[i+j+1]
				except:
					args[j] = None
	return args

def _when(date, format="%a %b %d %H:%M:%S %Y "):
	import time, calendar
	ar_date = date.rsplit(' ')
	new_date = ''
	for ar in ar_date:
		if ar[0] == '+' or ar[0] == '-':
			None
		else:
			new_date += ar + ' '
	then = calendar.timegm(time.strptime(new_date, format))
	now = time.time()
	delta = now - then
	for i, j in [[2592000, WRD_MON], [604800, WRD_WEK], [86400, WRD_DAY], [3600, WRD_HOR], [60, WRD_MIN]]:
		aux = int(delta/i)
		if aux > 1:
			return string.replace(WRD_AGO, "{time}", str(aux) + ' ' + j[1])
		elif aux == 1:
			return string.replace(WRD_AGO, "{time}", str(aux) + ' ' + j[0])
	return WRD_NOW

def _from(source):
	source = string.replace(string.replace(source, '&lt;', '<'),'&gt;', '>')
	if source[0] == "<":
		source = source[source[:-1].find(">")+1:source[1:].find("<")+1]
	return WRD_FRM + ' ' + source

def _divide(datos):
	sep=datos.split(':')
	if len(sep)==2:
		return sep
	else:
		return -1

def _html(text, to_html_code=False):
	i = 0
	j = 1
	if to_html_code:
		i = 1
		j = 0
	for code in HTML_CODES:
		text = text.replace(code[i],code[j])
	return text

#Program Start

if not ('-nc' in sys.argv or '--no-color' in sys.argv):
	COLOREAR()

print COL[1] + WRN_BETA + COL[10]
print "-------------------------------"

if '-h' in sys.argv or '--help' in sys.argv:
	arg = _arg(['-h','--help'],1)[0]
	if(arg and TXT_HELP_S.has_key(arg)):
		print TXT_HELP_S[arg]
	else:
		print TXT_HELP
	sys.exit(0)
elif '-v' in sys.argv or '--version' in sys.argv:
	print TXT_VERSION
	sys.exit(0)
elif '-s' in sys.argv or '--srch' in sys.argv:
	query = _arg(['-s','--srch'],1)[0]
	if '-lm' in sys.argv or '--lim' in sys.argv:
		query += "&rpp=" + _arg(['-lm','--lim'],1)[0]
	if '-pg' in sys.argv or '--pag' in sys.argv:
		query += "&page=" + _arg(['-pg','--pag'],1)[0]
	t_srch = tw_twitter.TwitterSearch(query)
	state, json_data = t_srch.search()
	if state != "ok":
		if state == "httpError":
			print COL[1] + _set_tags(ERR_HTTP, [('code', json_data)]) + COL[10]
		elif state == "urlError":
			print COL[1] + _set_tags(ERR_URL, [('code', json_data)]) + COL[10]
		sys.exit(0)
	for result in json_data['results']:
		print COL[3] + "[ID:" + str(result['id']) + "] " + COL[4] + _set_tags(HDR_SRCH_SAID, [('contact', result['from_user'])]) + COL[6] + " [" + _when(result['created_at'],"%a, %d %b %Y %H:%M:%S ") + " " + _from(result['source']) + "]" + COL[10]
		print "\t" + _html(result['text'])
		if result['geo']:
			print COL[2] + "\t*" + WRD_GTG + " [ " + str(result['geo']['coordinates'][0]) + " : " + str(result['geo']['coordinates'][1])+" ]" + COL[10]
elif '-a' in sys.argv or '--auth' in sys.argv:
	from tw_oauth import OAuthManager
	user = _arg(['-a','--auth'],1)[0]
	oauth = OAuthManager(user)
	print COL[6] + GMM_PIN + COL[10]
	request_token = oauth.create_request_token()
	print COL[3]
	key = raw_input("PIN: ")
	print COL[10]
	code = oauth.authorize(key,request_token)
	if code == 'ok':
		print COL[2] + ADV_SCCS_AUTH + COL[10]
	else:
		print COL[1] + ERR_AUTH + COL[10]
elif '-m' in sys.argv or '--mon' in sys.argv:
	user = _arg(['-m','--mon'],1)[0]
	dm = False
	t_mon = tw_twitter.TwitterMonitor(user)
	if '-us' in sys.argv or '--user' in sys.argv:
		t_mon.set_mode('us')
		t_mon.set_aux(_arg(['-us','--user'],1)[0])
	elif '-me' in sys.argv or '--mentions' in sys.argv:
		t_mon.set_mode('me')
	elif '-dr' in sys.argv or '--dir-rec' in sys.argv:
		dm = 'dr'
		t_mon.set_mode('dr')
	elif '-ds' in sys.argv or '--dir-sent' in sys.argv:
		dm = 'ds'
		t_mon.set_mode('ds')
	elif '-ll' in sys.argv or '--lists' in sys.argv:
		dm = 'll'
		t_mon.set_mode('ll')
	elif '-ls' in sys.argv or '--list' in sys.argv:
		dm = 'ls'
		t_mon.set_mode('ls')
		t_mon.set_aux(_arg(['-ls','--list'],1)[0])
	elif '-nr' in sys.argv or '--no-retweets' in sys.argv:
		t_mon.set_mode('nr')
	parm = {}
	if dm != 'll':
		if '-lm' in sys.argv or '--lim' in sys.argv:
			if dm == 'ls':
				parm['per_page'] = _arg(['-lm','--lim'],1)[0]
			else:
				parm['count'] = _arg(['-lm','--lim'],1)[0]
		if '-pg' in sys.argv or '--pag' in sys.argv:
			parm['page'] = _arg(['-pg','--pag'],1)[0]
	t_mon.set_params(parm)
	state, json_data = t_mon.monitor()
	if state != 'ok':
		if state == 'noToken':
			print COL[1] + _set_tags(ERR_NO_TOKN, [('user', user)]) + COL[10]
		if state == 'httpError':
			print COL[1] + _set_tags(ERR_HTTP, [('code', str(json_data))]) + COL[10]
		if state == 'urlError':
			print COL[1] + _set_tags(ERR_URL, [('code', str(json_data))]) + COL[10]
		sys.exit(0)
	if dm == 'dr':
		for msg in json_data:
			print COL[3] + "[ID:" + str(msg['id']) + "] " + COL[5] + _set_tags(HDR_TOLD_U, [('contact', msg['sender']['name']), ('contact_id', msg['sender']['screen_name'])]) + COL[6] + " [" + _when(msg['created_at']) + "]" + COL[10]
			print "\t" + msg['text']
	elif dm == 'ds':
		for msg in json_data:
			print COL[3] + "[ID:" + str(msg['id']) + "] " + COL[4] + _set_tags(HDR_U_TOLD, [('contact', msg['recipient']['name']), ('contact_id', msg['recipient']['screen_name'])]) + COL[6] + " [" + _when(msg['created_at']) + "]" + COL[10]
			print "\t" + msg['text']
	elif dm == 'll':
		for lst in json_data['lists']:
			print COL[3] + "[ID:" + str(lst['id']) + "] " + COL[6] + " [" + lst['mode'] + "]\t" + COL[4] + lst['name'] + COL[10]
	else:
		for status in json_data:
			#print status
			if status.has_key('retweeted_status'):
				o_status = status['retweeted_status']
				print COL[3] + "[ID:" + str(status['id']) + "] <- [ID:" + str(o_status['id']) + "] " +  COL[5] + _set_tags(HDR_RTD, [('rtd', o_status['user']['name']), ('rtd_id', o_status['user']['screen_name']), ('rtr', status['user']['name']), ('rtr_id', status['user']['screen_name'])]) + COL[6] + " [" + _when(status['created_at']) + " " + _from(status['source']) + "]" + COL[10]
			else:
				o_status = status
				print COL[3] + "[ID:" + str(status['id']) + "] " + COL[4] + _set_tags(HDR_CNTC_SAID, [('contact', status['user']['name']), ('contact_id', status['user']['screen_name'])]) + COL[6] + " [" + _when(status['created_at']) + " " + _from(status['source']) + "]" + COL[10]
			print "\t" + o_status['text']
			if o_status['geo']:
				print COL[2] + "\t*" + WRD_GTG + " [ " + str(o_status['geo']['coordinates'][0]) + " : " + \
					str(o_status['geo']['coordinates'][1])+" ]" + COL[10]
elif '-p' in sys.argv or '--post' in sys.argv:
	arg = _arg(['-p','--post'],2)
	user = arg[0]
	dm_at = False
	mensaje = string.replace(arg[1] ,"\!" ,"!")			#no se puede pasar desde terminal ! directamente
	t_post = tw_twitter.TwitterPost(user)
	if '-d' in sys.argv or '--dir-msg' in sys.argv:
		dm_at = _arg(['-d','--dir-msg'],1)[0]
		t_post.set_mode('d')
		print COL[6] + ADV_AS_DM + COL[4] + " @" + dm_at + COL[10]
	if '-u' in sys.argv or '--url' in sys.argv:
		url=_arg(['-u','--url'],1)[0]
		tiny_url = urllib.urlopen("http://tinyurl.com/api-create.php?url=" + url).read()
		if mensaje.find("{url}") == -1:
			mensaje += " " + str(tiny_url)
		else:
			try:
				mensaje = string.replace(_utf8(mensaje),"{url}",str(tiny_url))
			except:
				mensaje = string.replace(mensaje,"{url}",str(tiny_url))
		if len(mensaje)>140:
			print COL[1] + ERR_OVER_140 + COL[10]
			sys.exit(0)
		print COL[5] + _set_tags(ADV_SCCS_ADD,[('this', WRD_URL)]) + COL[4] + str(tiny_url) + COL[10]
	if '-i' in sys.argv or '--img' in sys.argv:
		if len(mensaje)>110:
			print COL[1] + _set_tags(ERR_CASE_110, ['media', WRD_IMG[1]]) + COL[10]
			sys.exit(0)
		if len(sys.argv)<5:
			print COL[1] + ERR_NO_PRMS + COL[10]
			sys.exit(0)
		arg=_arg(['-i','--img'],1)[0]
		if not psswd:
			print COL[3] + _set_tags(GMM_PSWD, [('media', WRD_IMG[0])]) + COL[10]
			psswd = getpass.getpass(":")
		import tw_twitpic
		twitpicker = tw_twitpic.TwitPicKer(user, psswd)
		print COL[3] + _set_tags(ADV_UPLD, [('media', WRD_IMG[0])]) + COL[10]
		upload_code, upload_url = twitpicker.upload(arg)
		if upload_code != 'ok':
			print COL[1] + _set_tags(ERR_UPLD, [('media', WRD_IMG[0]), ('code', upload_code), ('error', upload_url)]) + COL[10]
			sys.exit(0)
		print COL[5] + _set_tags(ADV_SCCS_ADD,[('this', WRD_IMG[0])]) + COL[4] + upload_url + COL[10]
		if mensaje.find("{img}") == -1:
			mensaje = upload_url + " : " + mensaje
		else:
			try:
				mensaje = string.replace(_utf8(mensaje),"{img}",upload_url)
			except:
				mensaje = string.replace(mensaje,"{img}",upload_url)
	if '-vd' in sys.argv or '--vid' in sys.argv:
		if len(mensaje)>110:
			print COL[1] + _set_tags(ERR_CASE_110, ['media', WRD_VID[1]]) + COL[10]
			sys.exit(0)
		if len(sys.argv)<5:
			print COL[1] + ERR_NO_PRMS + COL[10]
			sys.exit(0)
		arg=_arg(['-vd','--vid'],1)[0]
		if not psswd:
			print COL[3] + _set_tags(GMM_PSWD, [('media', WRD_VID[0])]) + COL[10]
			psswd = getpass.getpass(":")
		import tw_twitvid
		twitvidpicker = tw_twitvid.TwitVidPicker(user, psswd)
		if '-g' in sys.argv or '--geo' in sys.argv:
			geo=_divide(_arg(['-g','--geo'],1)[0])
			if geo != -1:
				twitvidpicker.set_geo(geo)
		print COL[3] + _set_tags(ADV_UPLD, [('media', WRD_VID[0])]) + COL[10]
		upload_code, upload_url = twitvidpicker.upload(arg)
		if upload_code != 'ok':
			print COL[1] + _set_tags(ERR_UPLD, [('media', WRD_VID[0]), ('code', upload_code), ('error', upload_url)]) + COL[10]
			sys.exit(0)
		print COL[5] + _set_tags(ADV_SCCS_ADD,[('this', WRD_VID[0])]) + COL[4] + upload_url + COL[10]
		if mensaje.find("{vid}") == -1:
			mensaje = upload_url + " : " + mensaje
		else:
			try:
				mensaje = string.replace(_utf8(mensaje),"{vid}",upload_url)
			except:
				mensaje = string.replace(mensaje,"{vid}",upload_url)
	if '-dc' in sys.argv or '--doc' in sys.argv:
		if len(mensaje)>110:
			print COL[1] + _set_tags(ERR_CASE_110, ['media', WRD_DOC[1]]) + COL[10]
			sys.exit(0)
		if len(sys.argv)<5:
			print COL[1] + ERR_NO_PRMS + COL[10]
			sys.exit(0)
		arg=_arg(['-dc','--doc'],1)[0]
		if not psswd:
			print COL[3] + _set_tags(GMM_PSWD, [('media', WRD_DOC[0])]) + COL[10]
			psswd = getpass.getpass(":")
		import tw_twitdoc
		twitdocpicker = tw_twitdoc.TwitDocPicker(user, psswd)
		print COL[3] + _set_tags(ADV_UPLD, [('media', WRD_DOC[0])]) + COL[10]
		upload_code, upload_url = twitdocpicker.upload(arg)
		if upload_code != 'ok':
			print COL[1] + _set_tags(ERR_UPLD, [('media', WRD_DOC[0]), ('code', upload_code), ('error', upload_url)]) + COL[10]
			sys.exit(0)
		print COL[5] + _set_tags(ADV_SCCS_ADD,[('this', WRD_DOC[0])]) + COL[4] + upload_url + COL[10]
		if mensaje.find("{doc}") == -1:
			mensaje = upload_url + " : " + mensaje
		else:
			try:
				mensaje = string.replace(_utf8(mensaje),"{doc}",upload_url)
			except:
				mensaje = string.replace(mensaje,"{doc}",upload_url)
	if dm_at:
		if len(sys.argv)<5:
			print COL[1] + ERR_NO_PRMS + COL[10]
			sys.exit(0)
		parm = {}
		parm['user'] = dm_at
		try:
			parm['text'] = mensaje.encode('utf8')
		except:
			parm['text'] = mensaje
	else:
		if len(sys.argv)<3:
			print COL[1] + ERR_NO_PRMS + COL[10]
			sys.exit(0)
		parm = {}
		try:
			parm['status'] = mensaje.encode('utf8')
		except:
			parm['status'] = mensaje
	if '-g' in sys.argv or '--geo' in sys.argv:
		if dm_at:
			print COL[3] + _set_tags(WRN_DM_NO,[('this', WRD_GTG)]) + COL[10]
		else:
			if len(sys.argv)<5:
				print COL[3] + ERR_NO_PRMS + COL[10]
				sys.exit(0)
			arg=_arg(['-g','--geo'],1)[0]
			geo=_divide(arg)
			if geo==-1:
				print COL[1] + ERR_GTAG_NDED + COL[10]
				sys.exit(0)
			parm['lat'] = geo[0]
			parm['long'] = geo[1]
			print COL[5] + _set_tags(ADV_SCCS_ADD,[('this', WRD_GTG)]) + COL[4] + "[ " + geo[0] + " : " + geo[1] + " ]" + COL[10]
	if '-ir' in sys.argv or '--inresponse' in sys.argv:
		if dm_at:
			print COL[3] +  _set_tags(WRN_DM_NO,[('this', WRD_RSP)]) + COL[10]
		else:
			if len(sys.argv)<5:
				print COL[3] + ERR_NO_PRMS + COL[10]
				sys.exit(0)
			arg=_arg(['-ir','--inresponse'],1)[0]
			parm['in_reply_to_status_id'] = arg
			print COL[5] + _set_tags(ADV_SCCS_ADD,[('this', WRD_RSP)]) + COL[4] + arg + COL[10]
	if len(mensaje)>140:
		print COL[1] + ERR_OVER_140 + COL[10]
		sys.exit(0)
	state, status = t_post.post(parm)
	if state != 'ok':
		if state == 'noToken':
			print COL[1] + _set_tags(ERR_NO_TOKN, [('user', user)]) + COL[10]
		if state == 'httpError':
			print COL[1] + _set_tags(ERR_HTTP, [('code', str(status))]) + COL[10]
		if state == 'urlError':
			print COL[1] + _set_tags(ERR_URL, [('code', str(status))]) + COL[10]
		sys.exit(0)
	if dm_at:
		print COL[3] + "[ID:" + str(status['id']) + "] " + COL[4] + _set_tags(HDR_U_TOLD, [('contact', status['recipient']['name']), ('contact_id', status['recipient']['screen_name'])]) + COL[6] + " [" + _when(status['created_at']) + "]" + COL[10]
		print "\t" + status['text']
		msg_suc = _set_tags(ADV_SCCS_WAS, [('done', WRD_SNT)]) + " @" + dm_at + "!"
	else:
		print COL[3] + "[ID:" + str(status['id']) + "] " + COL[4] + _set_tags(HDR_CNTC_SAID, [('contact', status['user']['name']), ('contact_id', status['user']['screen_name'])]) + COL[6] + " [" + _when(status['created_at']) + " " + _from(status['source']) + "]" + COL[10]
		print "\t" + status['text']
		if status['geo']:
			print COL[2] + "\t*" + WRD_GTG + " [ " + str(status['geo']['coordinates'][0]) + " : " + str(status['geo']['coordinates'][1])+" ]" + COL[10]
		msg_suc = _set_tags(ADV_SCCS_WAS, [('done', WRD_PST)]) + "!"
	print COL[2] + msg_suc + COL[10]
elif '-t' in sys.argv or '--twt' in sys.argv:
	arg = _arg(['-t','--twt'],2)
	user = arg[0]
	twid = arg[1]
	dd_bol = False
	t_twt = tw_twitter.TwitterTweet(user, twid)
	if '-rt' in sys.argv or '--retweet' in sys.argv:
		t_twt.set_mode('rt')
	elif '-dl' in sys.argv or '--delete' in sys.argv:
		t_twt.set_mode('dl')
	elif '-dd' in sys.argv or '--delete-dir' in sys.argv:
		dd_bol = True
		t_twt.set_mode('dd')
	else:
		print COL[1] + ERR_NO_OPTN + ". " + WRN_ONLY_PRMS + COL[10]
		sys.exit(0)
	state, status = t_twt.execute()
	if state != 'ok':
		if state == 'noToken':
			print COL[1] + _set_tags(ERR_NO_TOKN, [('user', user)]) + COL[10]
		if state == 'httpError':
			print COL[1] + _set_tags(ERR_HTTP, [('code', str(status))]) + COL[10]
		if state == 'urlError':
			print COL[1] + _set_tags(ERR_URL, [('code', str(status))]) + COL[10]
		sys.exit(0)
	if dd_bol:
		if status['recipient']['screen_name'] == user:
			print COL[3] + "[ID:" + str(status['id']) + "] " + COL[5] + _set_tags(HDR_TOLD_U, [('contact', status['sender']['name']), ('contact_id', status['sender']['screen_name'])]) + COL[6] + " [" + _when(status['created_at']) + "]" + COL[10]
		else:
			print COL[3] + "[ID:" + str(status['id']) + "] " + COL[4] + _set_tags(HDR_U_TOLD, [('contact', status['recipient']['name']), ('contact_id', status['recipient']['screen_name'])]) + COL[6] + " [" + _when(status['created_at']) + "]" + COL[10]
		print "\t" + status['text']
		msg_suc = _set_tags(ADV_SCCS_WAS, [('done', WRD_DLD)])
	else:
		if status.has_key('retweeted_status'):
			o_status = status['retweeted_status']
			print COL[3] + "[ID:" + str(status['id']) + "] <- [ID:" + str(o_status['id']) + "] " +  COL[5] + _set_tags(HDR_RTD, [('rtd', o_status['user']['name']), ('rtd_id', o_status['user']['screen_name']), ('rtr', status['user']['name']), ('rtr_id', status['user']['screen_name'])]) + COL[6] + " [" + _when(status['created_at']) + " " + _from(status['source']) + "]" + COL[10]
		else:
			o_status = status
			print COL[3] + "[ID:" + str(status['id']) + "] " + COL[4] + _set_tags(HDR_CNTC_SAID, [('contact', status['user']['name']), ('contact_id', status['user']['screen_name'])]) + COL[6] + " [" + _when(status['created_at']) + " " + _from(status['source']) + "]" + COL[10]
		print "\t" + o_status['text']
		if o_status['geo']:
			print COL[2] + "\t*" + WRD_GTG + " [ " + str(o_status['geo']['coordinates'][0]) + " : " + \
					str(o_status['geo']['coordinates'][1])+" ]" + COL[10]
		if '-rt' in sys.argv or '--retweet' in sys.argv:
			msg_suc = _set_tags(ADV_SCCS_WAS, [('done', WRD_RTD)])
		elif '-dl' in sys.argv or '--delete' in sys.argv:
			msg_suc = _set_tags(ADV_SCCS_WAS, [('done', WRD_DLD)])
	print COL[2] + msg_suc + COL[10]
else:
	print TXT_USAGE
print "-------------------------------"
sys.exit(0)
